package com.waimai.demo.Service.Serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waimai.demo.Dao.SetmealMapper;
import com.waimai.demo.Service.SetmealDishService;
import com.waimai.demo.Service.SetmealService;
import com.waimai.demo.domain.Dish;
import com.waimai.demo.domain.Setmeal;
import com.waimai.demo.domain.SetmealDish;
import com.waimai.demo.dto.SetmealDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;

    @Override
    public void savawithDish(SetmealDto setmealDto, HttpServletRequest request) {


        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        Long ids = (Long) request.getSession().getAttribute("employee");
        setmealDto.setCreateUser(ids);
        setmealDto.setUpdateUser(ids);
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            Long id = (Long) request.getSession().getAttribute("employee");
            setmealDish.setCreateUser(id);
            setmealDish.setUpdateUser(id);


        }
        setmealDishService.saveBatch(setmealDishes);
    }
}
